package com.example.librarymanagement.controller;

import com.example.librarymanagement.model.Admin;
import com.example.librarymanagement.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/admins")
public class AdminController {

    private final AdminService adminService;

    @Autowired
    public AdminController(AdminService adminService) {
        this.adminService = adminService;
    }

    @PostMapping("/login")
    public ResponseEntity<?> loginAdmin(@ModelAttribute Admin adminCredentials) {
        Admin admin = adminService.authenticateAdmin(adminCredentials);
        if (admin == null) {
            // 登录失败，返回404状态码和错误信息
            return ResponseEntity.status(HttpStatus.NOT_FOUND)
                    .body("{\"loginSuccess\":false,\"message\":\"Admin not found with provided credentials\"}");
        }
        // 登录成功，返回200状态码和成功消息
        Map<String, Object> response = new HashMap<>();
        response.put("loginSuccess", true);
        // 可以添加其他需要返回给前端的数据
        return ResponseEntity.ok(response);
    }
}